package com.ceshiren.springstudy.util;

import org.springframework.core.convert.ConversionFailedException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

//import top.testeru.ssmturorials.util.ErrorR;

import javax.servlet.http.HttpServletRequest;


//ControllerAdvice  AOP面向切面编程，对原来对功能没有侵入性，原来什么样子现在还是什么样子
//我只是在原来的功能的基础上给你扩展出一个功能，统一异常处理功能
//@ControllerAdvice
@RestControllerAdvice
public class GlobalExceptionHandler {

    String tips = "系统繁忙，请稍后重试";

    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler({Exception.class})
//    @ResponseBody
    public Result exceptionHandler(Exception e){
        //记录日志
        //通知运维
        //通知开发
        //控制台打印异常，万一出现异常调试
        e.printStackTrace();
        return Result.error().message("非业务异常 "+ tips);

    }


    @ExceptionHandler({ServiceException.class})
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Result servceExceptionHandler(HttpServletRequest req, ServiceException se){
        String requestUrl = req.getRequestURI();
        String method = req.getMethod();
        se.printStackTrace();
//        return new ErrorR(se.getCode(), "业务异常 "+ tips,method + " "+ requestUrl);
        return Result.error().message("业务异常 "+ tips +" RestAPI:"+method + " "+ requestUrl).code(se.getCode());
    }



    ////    .ArithmeticException
    @ExceptionHandler({ArithmeticException.class})
//    @ResponseBody
    public Result exceptionHandler1(ArithmeticException e){
        e.printStackTrace();
        return Result.error().message("特殊异常处理");
    }

}
